<?php


namespace App\backend\controllers\goods;


use App\backend\controllers\AdminBaseController;
use App\backend\models\GoodParamsModel;
use App\backend\services\GoodParamServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodParamsController extends AdminBaseController
{
    protected $class_name = 'GoodParams';

    /**
     * 商品参数列表
     */
    public function getList(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodParamsModel::rules(),
            GoodParamsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodParamServices::getList($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 添加/编辑商品参数
     */
    public function goodParamsModify(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodParamsModel::modify_rules(),
            GoodParamsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodParamServices::goodSkuModify($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 删除商品参数
     */
    public function goodParamsDelete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodParamsModel::delete_rules(),
            GoodParamsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodParamServices::goodParamsDelete($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 修改商品参数状态 1、显示  0、隐藏
     */
    public function goodParamsState(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodParamsModel::state_rules(),
            GoodParamsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodParamServices::goodParamsState($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 商品参数详情
     */
    public function goodParamsInfo(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodParamsModel::info_rules(),
            GoodParamsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodParamServices::goodParamsInfo($data);
        return successJson('操作成功！', $list);
    }

}
